package com.pkk.penguin.test.elasticsearch.controller;

import com.google.gson.Gson;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import com.pkk.penguin.test.elasticsearch.entity.Article;
import com.pkk.penguin.test.elasticsearch.repository.ArticleRepository;
import com.pkk.penguin.test.elasticsearch.service.ArticleService;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: peikunkun
 * @create: 2019-10-14 09:38
 **/
@RestController
@RequestMapping("es")
public class ArticleController {

  @Autowired
  private ArticleRepository articleRepository;
  @Autowired
  private ArticleService articleService;


  /**
   * 添加
   *
   * @return
   */
  @RequestMapping("add")
  public CommonResponse<String> add(String title, String content) {
    if (StringUtils.isBlank(title) || StringUtils.isBlank(content)) {
      return ResponseUtil.error("必传参数缺失");
    }
    Article article = new Article();
    article.setId(System.currentTimeMillis());
    article.setTitle("文章标题" + RandomUtils.nextInt());
    article.setContent("文章内容" + RandomUtils.nextInt());
    article.setDesc("文章描述" + RandomUtils.nextInt());
    article.setComments("文章评论" + RandomUtils.nextInt());
    articleRepository.save(article);
    System.err.println("add a obj");
    return ResponseUtil.success();
  }

  /**
   * 删除
   *
   * @return
   */
  @RequestMapping("delete")
  public CommonResponse<String> delete(Long id) {
    if (null == id) {
      return ResponseUtil.error("必传参数缺失");
    }
    Article article = articleRepository.queryArticleInfoById(id);
    articleRepository.delete(article);
    return ResponseUtil.success();
  }

  /**
   * 局部更新
   *
   * @return
   */
  @RequestMapping("update")
  public CommonResponse<String> update(Long id, String content) {
    if (null == id) {
      return ResponseUtil.error("必传参数缺失");
    }
    Article article = articleRepository.queryArticleInfoById(id);
    if (StringUtils.isNotBlank(content)) {
      article.setContent(content);
    } else {
      article.setContent("改变内容" + RandomUtils.nextInt());
    }
    articleRepository.save(article);
    System.err.println("update a obj");
    return ResponseUtil.success();
  }

  /**
   * 查询
   *
   * @return
   */
  @RequestMapping("query")
  public CommonResponse<Article> query(Long id) {
    if (null == id) {
      return ResponseUtil.error("必传参数缺失");
    }
    Article accountInfo = articleRepository.queryArticleInfoById(id);
    System.err.println(new Gson().toJson(accountInfo));
    return ResponseUtil.success(accountInfo);
  }


  /**
   * @Description: 查询信息
   * @Param: [title, content]
   * @return: com.pkk.penguin.test.elasticsearch.entity.Article
   * @Author: peikunkun
   * @Date: 2019/10/18 0018 下午 3:45
   */
  @RequestMapping("_query")
  public CommonResponse<List<Article>> queryTitilOrConent(String title, String content) {
    if (StringUtils.isBlank(title) || StringUtils.isBlank(content)) {
      return ResponseUtil.success(articleService.queryArticle(title, content).getContent());
    }
    System.err.println(new Gson().toJson(articleService.queryArticleByTitleContentBuild(title, content)));
    if (RandomUtils.nextBoolean()) {
      System.err.println("注解方式");
      return ResponseUtil.success(articleService.queryArticleByTitleContentAnnotation(title, content));
    } else {
      System.err.println("简单方式");
      return ResponseUtil.success(articleService.queryArticleByTitleContentSimple(title, content));
    }
  }


  /**
   * @Description: 查询信息
   * @Param: [title, content]
   * @return: com.pkk.penguin.test.elasticsearch.entity.Article
   * @Author: peikunkun
   * @Date: 2019/10/18 0018 下午 3:45
   */
  @RequestMapping("_querypg")
  public CommonResponse<Page<Article>> queryTitilOrConentByPage(String title, String content, Integer pageNum,
      Integer pageSize) {
    if (null == pageNum) {
      pageNum = 5;
    }
    if (null == pageSize) {
      pageNum = 1;
    }
    return ResponseUtil.success(articleService.queryArticlePage(title, content, pageNum, pageSize));
  }


}
